package com.zizy.healthexpert.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.zizy.healthexpert.Adapter.listHistoryAdapter;
import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Practical.Practical;

import java.util.ArrayList;

/**
 * Created by Zizy on 2014/7/1.
 */
public class MyDatabase_DishesRecords extends MyDataBase {
    private  final String TABLE_NAME="DishesRecords";
    public MyDatabase_DishesRecords(Context context) {
        super(context);
        initMyDatabase();
    }

    @Override
    public void initMyDatabase() {
        CreateTable();
    }

    @Override
    public ArrayList<Object> SearchDatabase(String selectWhat, String where_expression) {
        //TODO
        ArrayList<Object> historyList = new ArrayList<Object>();
        DishesRecords dishesRecords = null;

        Cursor cursor = db.rawQuery(String.format(
                "select %s from %s where %s;", selectWhat, TABLE_NAME,
                where_expression), null);
        while (cursor.moveToNext()) {
            dishesRecords = new DishesRecords(cursor.getInt(0),
                    cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
            historyList .add(dishesRecords);
        }
        cursor.close();
        return historyList;
    }
    @Override
    public ArrayList<Object> SearchDatabase(String selectWhat) {
        ArrayList<Object> historyList = new ArrayList<Object>();
        DishesRecords dishesRecords = null;
        Cursor cursor = db.rawQuery(String.format(
                "select %s from %s ;", selectWhat, TABLE_NAME
                 ), null);
        while (cursor.moveToNext()) {
            dishesRecords = new DishesRecords(cursor.getInt(0),
                    cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
            historyList .add(dishesRecords);
        }
        cursor.close();
        return historyList;
    }


    @Override
    public void insertDatabase(Object... objects) {
        String date= Practical.getTimeFormat();
        String nutritionFormat=(String)objects[0];//nutrition format
        int dishesTime=(Integer)objects[1]; //time
        String dishesNames=(String)objects[2];//names
        this.db.execSQL(String
                .format("insert into %s (PersonID,Date,NutritionFormat,DishesTime,DishesName) values('%s','%s','%s','%s','%s')",
                        this.TABLE_NAME,Config.userID,date, nutritionFormat,dishesTime,dishesNames));
    }
    @Override
    public  void updateDatabase(Object... objects) {

    }

    @Override
    public  void CreateTable() {
        try {
            String sql = "create table if not exists " + TABLE_NAME
                    + " (PersonID integer, " + "Date TEXT ," +"NutritionFormat TEXT ," +"DishesTime TEXT ,DishesName TEXT);";
            db.execSQL(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static class DishesRecords{
        private String dishesName;
        private int personId;
        private String date;
        private String nutritionFormat;
        private String dishesTime;

        public String getDishesName() {
            return dishesName;
        }

        public int getPersonId() {
            return personId;
        }

        public String getDate() {
            return date;
        }

        public String getNutritionFormat() {
            return nutritionFormat;
        }

        public String getDishesTime() {
            return dishesTime;
        }

        public DishesRecords(int personId, String date, String nutritionFormat, String dishesTime, String dishesName) {
            this.personId = personId;
            this.date = date;
            this.nutritionFormat = nutritionFormat;
            this.dishesTime = dishesTime;
            this.dishesName = dishesName;
        }
    }
}
